<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAdd">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        style="margin:15px 0"
        :data="goodsList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color:lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="item">
          <el-tag v-if="item.row.cat_level == '1'"></el-tag>
          <el-tag type="success" v-else-if="item.row.cat_level == '2'"></el-tag>
          <el-tag type="warning" v-else></el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="item">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"></el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querryInfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="querryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="30%">
      <el-form :model="addRuleForm" :rules="addRules" ref="addRuleFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            expand-trigger="hover"
            :options="parentCateList"
            v-model="selectOption"
            :props="cascaderProps"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { type } from 'os'
export default {
  data() {
    return {
      querryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 4
      },
      goodsList: [],
      total: 0,
      //为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 表示将当前列定义为模板列
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'isok'
        },
        {
          label: '操作',
          type: 'template',
          // 表示当前这一列使用的模板名称
          template: 'opt'
        }
      ],
      //   关闭表单弹窗
      addDialogVisible: false,
      // 添加分类表单数据对象
      addRuleForm: {
        // 将要添加的分类名称
        cat_name: '',
        cat_pid: '',
        // 分类登记默认是1级分类
        cat_level: ''
      },
      //表单验证
      addRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 4, max: 9, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      //父级分类列表
      parentCateList:[],
      //选中的那一项 
      selectOption:[],
      //配置选项
      cascaderProps:{
          label:"cat_name",
          value:"cat_id",
          children:"children",
      }
    }
  },

  methods: {
    async getCateList() {
      const { data: res } = await this.axios.get('categories', {
        params: this.querryInfo
      })
    //   console.log(res)
      if (res.meta.status == 200) {
        // 把数据列表，赋值给catelist
        this.goodsList = res.data.result
        // 为总数据条数赋值
        this.total = res.data.total
      }
      console.log(this.goodsList)
    },
    handleSizeChange(val) {
      console.log(val)
      this.querryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.querryInfo.pagenum = val
      this.getCateList()
    },
    // 展示增加弹框
    showAdd() {
      //先获取父级分类数据
      this.getParentCateList()
      // 弹出窗口
      this.addDialogVisible = true
    },
    //获取父级分类数据
    async getParentCateList() {
      const { data: res } = await this.axios.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.parentCateList = res.data

      console.log(this.parentCateList)
    },
    handleChange(){

    }
  },

  created() {
    this.getCateList()
  }
}
</script>

<style lang='less' scoped>
</style>
